package comm;

import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * ClassName: Supersede
 * Package: comm
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 * Date: 2023-08-13
 * Time: 17:09
 */


//两个线程交替打印 1-100
public class Supersede {

    private static int count;
    private static Object obj = new Object();

    public static void main(String[] args) {

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    synchronized (obj) {
                        obj.notify();
                        if (count <= 100) {
                            try {
                                Thread.sleep(100);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            System.out.println(Thread.currentThread().getName() + ": " + count);
                            count++;
                            try {
                                obj.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        } else {
                            break;
                        }
                    }
                }
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    synchronized (obj) {
                        obj.notify();
                        if (count <= 100) {
                            try {
                                Thread.sleep(100);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            System.out.println(Thread.currentThread().getName() + ": " + count);
                            count++;
                            try {
                                obj.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        } else {
                            break;
                        }
                    }
                }
            }
        }).start();

    }

}
